from datetime import datetime, date
from typing import List, Optional, Union
from pydantic import BaseModel, validator, Field


class BaseCustomerUser(BaseModel):
    id: int
    platform: Optional[int] = 1
    name: Optional[str] = None
    alias: Optional[str] = None
    phone: Optional[str] = None
    friend_phone: Optional[str] = None
    address: Optional[str] = None
    password: Optional[str] = None
    avatar: Optional[str] = None
    card_no: Optional[int] = 0
    birth: Optional[str] = None
    sex: Optional[int] = 1
    age: Optional[int] = 1
    parent: Optional[int] = 0
    identity: Optional[str] = None
    remark: Optional[str] = None
    role_id: Optional[int] = 1
    is_active: Optional[bool] = True
    is_user: Optional[bool] = False
    user_id: Optional[int] = 0
    created_at: Optional[datetime]
    updated_at: Optional[datetime]
    last_login: Optional[datetime]
    tabs:Optional[str] = Field("")


class CustomerUserCreate(BaseModel):
    name: Optional[str] = Field("微信用户",example="昵称")
    # password: str = Field(example="密码")
    card_no: Optional[int] = Field(0, description="卡号,不用传")
    is_active: Optional[bool] = True
    is_user: Optional[bool] = False
    user_id: Optional[int] = Field(0, description="系统用户")
    role_id: Optional[int] = Field(1, description="角色id")
    platform: Optional[int] = Field(1, description="平台id")
    alias: Optional[str] = Field(None, example="真实姓名")
    phone: Optional[str] = Field(None, example="手机号码")
    friend_phone: Optional[str] = Field(None, example="挚友手机号")
    address: Optional[str] = Field(None, example="联系地址")
    avatar: Optional[str] = Field(None, example="头像")
    birth: Optional[str] = Field(None, example="生日")
    sex: Optional[int] = Field(1, description="性别1男2女")
    age: Optional[int] = Field(0, description="年龄")
    parent: Optional[int] = Field(0, description="上级id")
    identity: Optional[str] = Field(None, example="身份证")
    remark: Optional[str] = Field(None, example="其他备注")
    open_id: Optional[str] = Field(None, example="openid")
    tabs: Optional[str] = Field(None, example="标签")

    @validator("name")
    def validate_name_length(cls, v):
        if v is None:
            raise ValueError("姓名必填")
        return v


class CustomerUserUpdate(BaseModel):
    id: int
    name: str = Field(example="昵称")
    is_active: Optional[bool] = True
    is_user: Optional[bool] = False
    user_id: Optional[int] = 0
    role_id: Optional[int] = Field(1, description="角色id")
    platform: Optional[int] = Field(1, description="平台id")
    alias: Optional[str] = Field("", example="真实姓名")
    phone: Optional[str] = Field("", example="手机号码")
    friend_phone: Optional[str] = Field("", example="挚友手机号")
    address: Optional[str] = Field("", example="联系地址")
    avatar: Optional[str] = Field("", example="头像")
    birth: Optional[str] = Field("", example="生日")
    sex: Optional[int] = Field(1, description="性别1男2女")
    age: Optional[int] = Field(0, description="年龄")
    parent: Optional[int] = Field(0, description="上级id")
    identity: Optional[str] = Field("", example="身份证")
    remark: Optional[str] = Field("", example="其他备注")
    tabs: Optional[str] = Field(None, example="标签")


class BaseCustomerLevel(BaseModel):
    name: str = Field(..., description="等级名称", example="等级名称1")
    value: int = Field(0, description="等级ID")
    level_integral: int = Field(0, description="升级积分")


class CustomerLevelCreate(BaseCustomerLevel):
    ...


class CustomerLevelUpdate(BaseCustomerLevel):
    id: int

    def update_dict(self):
        return self.model_dump(exclude_unset=True, exclude={"id"})


class BaseCustomerGrow(BaseModel):
    name: str = Field(..., description="模板名称", example="模板名称1")
    type: int = Field(0, description="1固定比例，固定金额")
    routers: Optional[str] = Field("", description="规则")


class CustomerGrowCreate(BaseCustomerGrow):
    routers: Optional[list] = None


class CustomerGrowUpdate(BaseCustomerGrow):
    id: int
    routers: Optional[list] = None

    def update_dict(self):
        return self.model_dump(exclude_unset=True, exclude={"id"})


class CredentialsSchema(BaseModel):
    name: str = Field(..., description="手机号", example="18100616738")
    password: str = Field(..., description="密码", example="230307")


class CustomerJWTOut(BaseModel):
    access_token: str
    username: str


class CustomerJWTPayload(BaseModel):
    user_id: int
    username: str
    exp: datetime


class CustomerWeichatOpenid(BaseModel):
    code: str
    encryptedData: str
    iv: str


class CustomerWeichatLogin(BaseModel):
    info: str = Field(..., description="会员信息", example="")
    open_id: str = Field(..., description="open_id", example="MvEo7Y9xofKj5nZa5EQdmfbeV5s")
    union_id: str = Field(..., description="union_id", example="MvEo7Y9xofKj5nZa5EQdmfbeV5s")
    phone: str = Field(..., description="手机号", example="15051850953")
    user_id: int = Field(..., description="推荐人id", example=1)


class CustomerMod(BaseModel):
    id: Optional[int] = Field(0, description="客户id")
    name: str = Field(example="昵称")
    role_id: Optional[int] = Field(1, description="角色id")
    platform: Optional[int] = Field(1, description="平台id")
    alias: Optional[str] = Field("", example="真实姓名")
    phone: Optional[str] = Field("", example="手机号码")
    friend_phone: Optional[str] = Field("", example="挚友手机号")
    address: Optional[str] = Field("", example="联系地址")
    avatar: Optional[str] = Field("", example="头像")
    birth: Optional[str] = Field("", example="生日")
    parent_id: Optional[int] = Field("", example="邀请人id")
    sex: Optional[int] = Field(1, description="性别1男2女")
    age: Optional[int] = Field(0, description="年龄")
    identity: Optional[str] = Field("", example="身份证")
    remark: Optional[str] = Field("", example="其他备注")
    tabs: Optional[str] = Field(None, example="标签")



class BaseCustomerShare(BaseModel):
    user_id: Optional[int] = Field(0, description="分享人")
    share_url: Optional[str] = Field("", description="分享链接")
    click_time: Optional[int] = Field(0, description="阅读次数")

class CustomerShareCreate(BaseCustomerShare):
    ...

class CustomerShareUpdate(BaseCustomerShare):
    id:int

    def update_dict(self):
        return self.model_dump(exclude_unset=True, exclude={"id"})

class BaseCustomerVisit(BaseModel):
    customer_id: Optional[int] = Field(0, description="会员id")
    plan_id: Optional[int] = Field(0, description="计划id")
    add_uid: Optional[int] = Field(0, description="添加人id")
    remark: Optional[str] = Field("", description="备注")
    remark_images: Optional[list] = Field(None, description="回访照片")

class CustomerVisitCreate(BaseCustomerVisit):
    ...

class CustomerVisitUpdate(BaseCustomerVisit):
    id:int

    def update_dict(self):
        return self.model_dump(exclude_unset=True, exclude={"id"})